#ifndef CVS_THREAD_POOL_
#define CVS_THREAD_POOL_
#include "Thread.h"
#include <deque>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>

namespace cvs {
	class ThreadPool : boost::noncopyable {
	public:
		typedef boost::function<void ()> Task;

	public:
		explicit ThreadPool(const std::string& name = std::string());
		~ThreadPool();

		void start(int numThreads);
		void stop();

		void run(const Task& task);
	
	private:
		void runInThread(void);
		Task take(void);
	
	private:
		mutable boost::mutex mutex_;
		boost::condition_variable_any cond_;
		std::vector<cvs::Thread*> threads_;
		std::deque<Task> queue_;
		std::string name_;
		bool running_;
	};
}

#endif